//package com.easyexcle.demo.excleUtil;
//
//import com.alibaba.excel.EasyExcel;
//import com.alibaba.excel.ExcelWriter;
//import com.alibaba.excel.support.ExcelTypeEnum;
//import com.alibaba.excel.write.builder.ExcelWriterBuilder;
//import com.alibaba.excel.write.metadata.WriteSheet;
//import com.easyexcle.demo.easyExcel.style.CustomSheetWriteHandler;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletResponse;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.net.URLEncoder;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.CompletableFuture;
//
///**
// * @Author: lkz
// * @Title:
// * @Description:
// * @Date: 2023/4/29 23:44
// */
//
//public class JianfanEasyExcelUtil {
//
//    package com.jianfan.module.edc.util;
//
//import cn.hutool.extra.spring.SpringUtil;
//import com.alibaba.excel.EasyExcel;
//import com.alibaba.excel.ExcelWriter;
//import com.alibaba.excel.support.ExcelTypeEnum;
//import com.alibaba.excel.write.builder.ExcelWriterBuilder;
//import com.alibaba.excel.write.metadata.WriteSheet;
//import com.jianfan.framework.common.exception.ErrorCode;
//import com.jianfan.framework.common.exception.util.ServiceExceptionUtil;
//import com.jianfan.module.edc.enums.num.NumYESNoEnums;
//import com.jianfan.module.edc.excel.style.base.CustomSheetWriteHandler;
//import com.jianfan.module.edc.vo.statistic.dto.SheetDTO;
//import com.jianfan.module.system.api.file.FileApi;
//import org.checkerframework.checker.units.qual.C;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletResponse;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//
//import java.net.URLEncoder;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.CompletableFuture;
//import java.util.concurrent.CopyOnWriteArrayList;
//import java.util.concurrent.atomic.AtomicReference;
//
//    /**
//     * @Author: lkz
//     * @Title: EasyExcelUtils
//     * @Description: TODO
//     * @Date: 2023/4/12 11:58
//     */
//    @Component
//    public class EasyExcelUtils {
//
//
//        /**
//         * 单个表格数据导出
//         * @param response
//         * @param sheetFileName sheet名
//         * @param classVo 表格实体对象
//         * @param dataList 表格数据
//         * @throws IOException
//         */
//        public  static void responseExcel(HttpServletResponse response, String sheetFileName, Integer no,Class<? extends Object> classVo,
//                                          List<? extends Object> dataList)
//                throws IOException {
//            response.setContentType("application/vnd.ms-excel");
//            response.setCharacterEncoding("utf-8");
//            // 防止中文乱码
//            String fileName = URLEncoder.encode(sheetFileName, "UTF-8")
//                    .replaceAll("\\+","%20");
//            response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + fileName + ExcelTypeEnum.XLSX.getValue());
//            //响应的输入流
//            ServletOutputStream outputStream = response.getOutputStream();
//            // workbook
//            ExcelWriterBuilder writeWorkBook = EasyExcel.write(outputStream, classVo).head(classVo)
//                    .useDefaultStyle(false)
//                    .registerWriteHandler(new CustomSheetWriteHandler());
//            // sheet
//            writeWorkBook.sheet().sheetName(sheetFileName).sheetNo(no).doWrite(dataList);
//        }
//
//        /**
//         * 多个sheet导出
//         * @param response
//         * @param fileName 表格名称
//         * @param dataList 表格数据
//         * @throws IOException
//         */
//        public  static void responseBatchExcel(HttpServletResponse response,String fileName,
//                                               List<SheetDTO> dataList)
//                throws IOException {
//            response.setContentType("application/vnd.ms-excel");
//            response.setCharacterEncoding("utf-8");
//            // 防止中文乱码
//            String newFileName = URLEncoder.encode(fileName, "UTF-8")
//                    .replaceAll("\\+","%20");
//            response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + newFileName + ExcelTypeEnum.XLSX.getValue());
//            //响应的输入流
//            ServletOutputStream outputStream = response.getOutputStream();
//            // workbook
//            ExcelWriter writeWorkBook = EasyExcel.write(outputStream).inMemory(Boolean.TRUE)
//                    .useDefaultStyle(false)
//                    .registerWriteHandler(new CustomSheetWriteHandler()).build();
//            List<CompletableFuture> futures=new ArrayList<>();
//            try {
//            for(SheetDTO dto:dataList){
//                     Integer index = dto.getIndex();
//                CompletableFuture<Void> async = CompletableFuture.runAsync(() -> {
//                    WriteSheet writeSheet = new WriteSheet();
//                    if (dto.getType().equals(NumYESNoEnums.ONE.getNum())) {
//
//                        writeSheet = EasyExcel.writerSheet(index, dto.getFileName()).head(dto.getClassType()).build();
//                    } else if (dto.getType().equals(NumYESNoEnums.TWO.getNum())) {
//                        writeSheet = EasyExcel.writerSheet(index, dto.getFileName()).needHead(false).build();
//                    } else if (dto.getType().equals(NumYESNoEnums.THREE.getNum())) { // 动态表头
//                                    writeSheet = EasyExcel.writerSheet(index, dto.getFileName()).head(dto.getHeadList()).build();
//                    }
//                    synchronized (writeWorkBook) {
//                        writeWorkBook.write(dto.getDataList(), writeSheet);
//                    }
//                });
//                futures.add(async);
//            }
////
////            for (CompletableFuture completableFuture : futures) {
////                completableFuture.join();
////            }
//                // 不采取多线程
//                WriteSheet writeSheet=null;
//                for(SheetDTO dto:dataList){
//                    writeSheet=new WriteSheet();
//                    if(dto.getType().equals(NumYESNoEnums.ONE.getNum())){
//                        writeSheet = EasyExcel.writerSheet(dto.getIndex(), dto.getFileName()).head(dto.getClassType()).build();
//                    }else if(dto.getType().equals(NumYESNoEnums.TWO.getNum())){
//                        writeSheet = EasyExcel.writerSheet(dto.getIndex(), dto.getFileName()).needHead(false).build();
//                    }
//
//                    writeWorkBook.write(dto.getDataList(),writeSheet);
//                }
////        WriteSheet writeSheet = EasyExcel.writerSheet(0, "公共访视").needHead(false).build();
////        WriteSheet writeSheet2 = EasyExcel.writerSheet(1, "非公共访视").needHead(false).build();
////        writeWorkBook.write(dataList.get(0).getDataList(),writeSheet);
////        writeWorkBook.write(dataList.get(1).getDataList(),writeSheet2);
//
//
//
//            }catch (Exception e){
//                throw ServiceExceptionUtil.exception(new ErrorCode(9999,"数据导出异常"));
//            }finally {
//                writeWorkBook.finish();
//                outputStream.flush();
//                outputStream.close();
//            }
//
//
//        }
//
//        /**
//         * 上传文件服务器
//         * @param fileName
//         * @param dataList
//         * @throws IOException
//         */
//        public  static String uploadResponseBatchExcel(String fileName,
//                                                       List<SheetDTO> dataList)
//                throws IOException {
//            //响应的输入流
//            ByteArrayOutputStream stream = new ByteArrayOutputStream();
//            String filePath=null;
//            // workbook
//            ExcelWriter writeWorkBook = EasyExcel.write(stream)
//                    .useDefaultStyle(false)
//                    .registerWriteHandler(new CustomSheetWriteHandler()).build();
//
//            for(SheetDTO dto:dataList){
//                WriteSheet writeSheet=new WriteSheet();
//                if(dto.getType().equals(NumYESNoEnums.ONE.getNum())){
//                    writeSheet = EasyExcel.writerSheet(dto.getIndex(), dto.getFileName()).head(dto.getClassType()).build();
//                    writeWorkBook.write(dto.getDataList(),writeSheet);
//                }else if(dto.getType().equals(NumYESNoEnums.TWO.getNum())){
//                    writeSheet = EasyExcel.writerSheet(dto.getIndex(), dto.getFileName()).needHead(false).build();
//                    writeWorkBook.write(dto.getDataList(),writeSheet);
//                }
//
//            }
//            writeWorkBook.finish();
//            FileApi fileApi = SpringUtil.getBean(FileApi.class);
//            String newFileName=fileName+System.currentTimeMillis()+ExcelTypeEnum.XLSX.getValue();
//            String path="edc/excel/"+newFileName;
//            filePath = fileApi.createFile(newFileName, path, stream.toByteArray());
//            stream.close();
//            return filePath;
//        }
//
//        /**
//         *  单个无表头导出
//         * @param response
//         * @param fileName sheet名
//         * @param classDto 导出对象
//         * @param dataList 数据集合
//         * @throws IOException
//         */
//        public  static void responseNoNeedHeadExcel(HttpServletResponse response, String fileName,
//                                                    Class<? extends Object> classDto, List< ? extends Object> dataList)
//                throws IOException {
//            response.setContentType("application/vnd.ms-excel");
//            response.setCharacterEncoding("utf-8");
//            // 防止中文乱码
//            String newFileName = URLEncoder.encode(fileName, "UTF-8")
//                    .replaceAll("\\+","%20");
//            response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + newFileName + ExcelTypeEnum.XLSX.getValue());
//            //响应的输入流
//            ServletOutputStream outputStream = response.getOutputStream();
//            // workbook
//            ExcelWriter writeWorkBook = EasyExcel.write(outputStream)
//                    .useDefaultStyle(false)
//                    .registerWriteHandler(new CustomSheetWriteHandler()).build();
//            WriteSheet writeSheet = EasyExcel.writerSheet(0, fileName).needHead(false).build();
//            writeWorkBook.write(dataList,writeSheet);
//
//            writeWorkBook.finish();
//            outputStream.close();
//        }
//
//
//
//    }
//
//
//}
